package com.aaa.tmr.controller;

import com.aaa.tmr.entity.Dept;
import com.aaa.tmr.service.DeptService;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 部门表(Dept)表控制层
 *
 * @author makejava
 * @since 2021-03-19 15:28:08
 */
@Controller
@RequestMapping("dept")
public class DeptController {
    /**
     * 服务对象
     */
    @Resource
    private DeptService deptService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Dept selectOne(Integer id) {
        return this.deptService.queryById(id);
    }

    /**
     * 跳转部门分页列表页面
     * @return
     */
    @RequestMapping("toPage")
    public String toPage(){
        return "back/dept-page";
    }

    /**
     * 分页查询方法
     * @param page
     * @param limit
     * @param dept
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page,Integer limit,Dept dept){
        PageInfo pageInfo = deptService.queryAll(page, limit, dept);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 部门添加
     * @param dept
     * @return
     */
    @ResponseBody
    @PostMapping("insert")
    public Result insert(Dept dept){
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0,deptService.insert(dept));
    }

    /**
     * 部门更新
     * @param dept
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public Result update(Dept dept){
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0,deptService.update(dept));
    }

    /**
     * 通过主键删除数据
     * @param deptId 主键
     * @return 是否成功
     */
    @ResponseBody
    @DeleteMapping("deleteById")
    public Result deleteById(Integer deptId){
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0,this.deptService.deleteById(deptId));
    }

    /**
     * 通过主键删除数据
     * @param deptIds
     * @return
     */
    @ResponseBody
    @DeleteMapping("deleteByIds")
    public Result deleteByIds(String deptIds){
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0,this.deptService.batchDeleteById(deptIds));
    }
}
